events {
    worker_connections  1000;
}

http {

    log_format  main  '$remote_addr - $remote_user [$time_local] $scheme $http_host $server_port "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" '
                  '$request_time $upstream_response_time $time_iso8601 ';

    server_names_hash_bucket_size   128;
    client_header_buffer_size       256k;
    large_client_header_buffers     32 256k;
    client_max_body_size            5120m;
    sendfile                        on;
    tcp_nopush                      on;
    keepalive_timeout               0;
    tcp_nodelay                     on;
    client_body_buffer_size         512k;
    fastcgi_intercept_errors        on;
    proxy_connect_timeout           500s;
    proxy_read_timeout              18000s;
    proxy_send_timeout              18000s;
    proxy_buffer_size               256k;
    proxy_buffers                   4 256k;
    proxy_busy_buffers_size         256k;
    proxy_temp_file_write_size      256k;
    proxy_intercept_errors          on;
    server_name_in_redirect         off;
    proxy_hide_header               X-Powered-By;
    ignore_invalid_headers          off;    # important

    upstream masters {
        server 192.168.0.11:17010;
        server 192.168.0.12:17010;
        server 192.168.0.13:17010;
    }

    upstream objectnodes {
        server 192.168.0.41:80 max_fails=0 fail_timeout=1s;
        server 192.168.0.42:80 max_fails=0 fail_timeout=1s;
        server 192.168.0.43:80 max_fails=0 fail_timeout=1s;
    }

    upstream consoles {
        server 192.168.0.50:80;
    }

    upstream grafana {
        server 192.168.0.103:3000;
    }

    upstream prometheus {
        server 192.168.0.102:9090;
    }

    server {
        listen 80;
        server_name object.cube.io ~^(?<bucket>.+)\.object\.cube\.io$;
        charset utf-8;

        location / {
            proxy_pass http://objectnodes;

            proxy_http_version  1.1;
            proxy_set_header    Host $http_host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Expect $http_Expect;
            proxy_next_upstream http_502 http_503 http_504 error timeout invalid_header;

            access_log  /tmp/object_access.log main;
            error_log  /tmp/object_error.log warn;
        }
    }

  server {
    listen 80;
    server_name console.cube.io;
    charset utf-8;

    location / {
      proxy_pass http://consoles;

        proxy_http_version 1.1;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log  /tmp/console_access.log main;
      error_log  /tmp/console_error.log warn;
    }
  }

  server {
    listen 80;
    server_name grafana.cube.io;
    charset utf-8;

    location / {
      proxy_pass http://grafana;

        proxy_http_version 1.1;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log  /tmp/console_access.log main;
      error_log  /tmp/console_error.log warn;
    }
  }

  server {
    listen 80;
    server_name prometheus.cube.io;
    charset utf-8;

    location / {
      proxy_pass http://prometheus;

        proxy_http_version 1.1;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log  /tmp/console_access.log main;
      error_log  /tmp/console_error.log warn;
    }
  }

    server {
        listen 80;
        server_name master.cube.io;
        charset utf-8;

        location / {
            proxy_pass http://masters;

            proxy_http_version  1.1;
            proxy_set_header    Host $http_host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

            access_log  /tmp/master_access.log main;
            error_log  /tmp/master_error.log warn;
        }
    }
}
